package edu.yuhf.web.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONObject;

import edu.yuhf.dao.HobbyJdbcDaoImpl;
import edu.yuhf.dao.iface.HobbyDao;
import edu.yuhf.domain.Hobby;

@WebServlet("/hobby.servlet")
public class HobbyServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	private HobbyDao hobbyDao=new HobbyJdbcDaoImpl(); 
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html; charset=utf-8");
		
		String param=null==request.getParameter("param")?"":request.getParameter("param");
		
		PrintWriter out=response.getWriter();
		Map<String,Object> map=new HashMap<String,Object>();
		if("init".equals(param)) {
			List<Hobby> list=hobbyDao.getHobbyAll();
			if(list.size()!=0) {
				map.put("result", "success");
				map.put("list", list);
			}
		}
		String jsonString=JSONObject.toJSONString(map);
		out.print(jsonString);
		out.flush();
		out.close();
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
